<template>
    <div>
        <Breadcrumb :style="{margin: '20px 0'}">
            <BreadcrumbItem>终端管理</BreadcrumbItem>
            <BreadcrumbItem>添加手机</BreadcrumbItem>
        </Breadcrumb>
        <ButtonGroup class="button">
            <Button type="primary" @click="scan">扫描</Button>
        </ButtonGroup>
        <Table :columns="columns" :data="deviceList"></Table>
        <Modal
            v-model="modalShow"
            title="设置和添加"
            @on-ok="save">
            <div class="field">
                <div class="label">是否真实终端(真机）</div>
                <Select v-model="phoneInfo.isReal" class="mb-10" value="1">
                    <Option value="1">是</Option>
                    <Option value="0">否</Option>
                </Select>
            </div>
            <div class="field">
                <div class="label">名称 </div>
                <Input v-model="phoneInfo.name" class="mb-10" placeholder="名称"/>
            </div>
            <div class="field">
                <div class="label">终端IP地址(如果是虚拟机可以不填）</div>
                <Input v-model="phoneInfo.ip" class="mb-10" placeholder="手机的ip地址"/>
            </div>
            <div class="field">
                <div class="label">
                    设备号(不知道的可以留空)
                </div>
                <Input v-model="phoneInfo.device" class="mb-10" placeholder="设备号(不知道的可以留空)"/>
            </div>
            <div class="field">
                <div class="label">
                    连接终端的端口（必选，并且唯一)
                </div>
                <Select v-model="phoneInfo.port" class="mb-10">
                    <Option v-for="item in portList" :key="item.value" :value="item.value">{{ item.label }}</Option>
                </Select>
            </div>

        </Modal>
    </div>

</template>

<script>
let process = require('child_process')
import model from '../util/model'

export default {
    name: 'DeviceList',
    data: function () {
        return {
            columns: [
                {
                    title: '序号',
                    key: 'index'
                },
                {
                    title: '设备号',
                    key: 'device'
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 300,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.add(params.index)
                                    }
                                }
                            }, '添加')
                        ])
                    }
                }
            ],
            deviceList: [],
            modalShow: false,
            phoneInfo: {
                name: '',
                ip: '',
                device: '',
                port: null,
                isReal:1
            },
            portList: [],
        }
    },
    created: function () {

    },
    methods: {
        scan: function () {
            let that = this
            let newStdout = []
            let deviceArr = []
            let valueTemp = ''
            let valueTemp2 = ''
            let deviceNumberArr = []
            let item = {
                index: '',
                device: ''
            }
            process.exec('adb devices', function (error, stdout, stderr) {
                if (stdout) {
                    newStdout = stdout.split('\n')
                    deviceArr = newStdout.slice(1, -1)
                    if (deviceArr.length > 0) {
                        let testValue = deviceArr[0].replace(/[\r\n]*/g, '')
                        if (testValue !== '') {
                            deviceArr.forEach((value, index) => {
                                valueTemp2 = value.replace(/[\r\n]*/g, '')
                                if (valueTemp2 !== '') {
                                    valueTemp = valueTemp2.split('\t')
                                    item = {}
                                    item.index = index
                                    item.device = valueTemp[0]
                                    deviceNumberArr.push(item)
                                }
                            })
                        } else {
                            that.$Message.info('没有找到设备')
                        }
                        that.deviceList = deviceNumberArr
                    } else {
                        that.$Message.info('没有找到设备')
                    }
                } else {
                    that.$Message.info('没有找到设备')
                }
            })
        },
        add: function (index) {
            let that = this
            let deviceInfo = this.deviceList[index]
            console.log(deviceInfo)
            let deviceData = model.info.find({device: deviceInfo.device},(error,doc)=>{
                if(doc.length > 0){
                    that.$Message.info('终端已存在，不用添加')
                }else{
                    this.phoneInfo.device = deviceInfo.device
                    this.phoneInfo.name = deviceInfo.device
                    this.getPortData()
                    this.modalShow = true
                }
            })
        },
        getPortData: function () {
            let that = this
            let item = {
                value: 5556,
                label: 5656
            }
            let start = 5556
            let end = 5656
            let temp = []
            model.port.find({}, function (err, cet) {
                start = parseInt(cet[0].start)
                end = parseInt(cet[0].end)
                for (let i = start; i < end; i++) {
                    item = {}
                    item.value = i
                    item.label = i
                    temp.push(item)
                }
            })
            that.portList = temp
        },
        save: function () {
            let that = this
            model.info.find({device: this.phoneInfo.device}, function (err, docs) {
                if (docs.length === 0) {
                    model.info.insert(that.phoneInfo, function (err, newDoc) {
                        if (newDoc) {
                            that.$Message.info('添加成功')
                        }
                    })
                } else {
                    model.info.update({device: that.phoneInfo.device}, {$set: that.phoneInfo}, {multi: true}, function (err, newDoc) {
                        if (newDoc) {
                            that.$Message.info('修改成功')
                        }
                    })
                }
            })
        }
    }
}
</script>

<style scoped>
.field{
    margin-bottom: 10px;
}
.label{
    margin-bottom:5px;
    color: #424242
}
.button {
    margin-bottom: 20px;
}

.mb-10 {
    margin-bottom: 10px;
}
</style>
